package com.clothing.entity;

import com.clothing.CommonAttributes;
import com.clothing.utils.Utils;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: Industry 
 * @Description: TODO(行业) 
 * @author Charles 
 * @date 2018年5月27日 上午10:59:39 
 * @version 1.0
 */
@Entity
@Table(name = "industry")
public class Industry extends BaseEntity
{
    /** 
     * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么) 
     */ 
    private static final long serialVersionUID = 8398055517433388588L;
    
    private String name;

    private String icon;

    private Integer level;

    private Industry parent;

    private List<Industry> children = new ArrayList<>();

    @Column(name = "name", nullable = false)
    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    @Column(name = "icon", nullable = false)
    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        if (!Utils.isEmpty(icon))
        {
            if (icon.startsWith(CommonAttributes.SITE_URL))
            {
                icon = icon.substring(CommonAttributes.SITE_URL.length(), icon.length());
            }
        }
        this.icon = icon;
    }

    @Column(name = "level", nullable = false)
    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent")
    public Industry getParent() {
        return parent;
    }

    public void setParent(Industry parent) {
        this.parent = parent;
    }

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    public List<Industry> getChildren() {
        return children;
    }

    public void setChildren(List<Industry> children) {
        this.children = children;
    }

    /**
     * 完整路径icon
     *
     * @return
     */
    @Transient
    public String getFullIcon() {
        return CommonAttributes.SITE_URL + icon;
    }
}
